<?php

class ctime{
	public $format;
	public $time_difference = 0;

	function __construct($timezone=8) {
		$this->time_difference = $timezone*3600;
	}

	//参数定制的时间
	function custom($format='Y-m-d H:i:s') {
		return date($format,time()+$this->time_difference);
	}

	//完整时间
	function long_time() {
		return date("Y-m-d H:i:s",time()+$this->time_difference);
	}

	//短时间
	function short_time() {
		return date("Y-m-d",time()+$this->time_difference);	
	}

	//时间戳
	function time_stamp() {
		return time()+$this->time_difference;
	}

	//当天开始时间戳和结束时间戳
	function day_begin_end($year=0,$month=0,$day=0,$format='') {
	    if(!( $year && $month && $day)) {
			$year = date("Y");
			$month = date("m");
			$day = date("d");		
		}
		$dayBegin = mktime(0,0,0,$month,$day,$year);//当天开始时间戳
		$dayEnd = mktime(23,59,59,$month,$day,$year);//当天结束时间戳		
		if($format) {
		    if($format==true){
				$dayBegin = date("Y-m-d H:i:s",$dayBegin);
				$dayEnd = date("Y-m-d H:i:s",$dayEnd);
            }else {
				$dayBegin = date($format,$dayBegin);
				$dayEnd = date($format,$dayEnd);
            }			
		}
		return array($dayBegin,$dayEnd);
	}	
	
    /* 日期格式显示 */
    function tdate($time, $type = 3, $friendly=1,$date_format='Y/m/d',$time_format='H:i',$time_friendly='1',$time_offset='8',$time_diff=0) {
        ($time_friendly != 1) && $friendly = 0;
        $format[] = $type & 2 ? (!empty($date_format) ? $date_format : 'Y-n-j') : '';
        $format[] = $type & 1 ? (!empty($time_format) ? $time_format : 'H:i') : '';
        $timeoffset = $time_offset * 3600 + $time_diff * 60;
        $timestring = gmdate(implode(' ', $format), $time + $timeoffset);
        if ($friendly) {
            $time = time() - $time;
            if ($time <= 24 * 3600) {
                if ($time > 3600) {
                    $timestring = intval($time / 3600) . '小时前';
                } elseif ($time > 60) {
                    $timestring = intval($time / 60) . '分钟前';
                } elseif ($time > 0) {
                    $timestring = $time . '秒前';
                } else {
                    $timestring = '现在前';
                }
            }
        }
        return $timestring;
    }
	//计算时间长度
    function timeLength($time) {
        $length = '';
        if ($day = floor($time / (24 * 3600))) {
            $length .= $day . '天';
        }
        if ($hour = floor($time % (24 * 3600) / 3600)) {
            $length .= $hour . '小时';
        }
        if ($day == 0 && $hour == 0) {
            $length = floor($time / 60) . '分';
        }
        return $length;
    }	
}
?>